/**
 * Project: invoicing
 * 
 * File Created at 2013-1-15
 * $Id$
 * 
 * Copyright 2008 invoicing.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * invoicing Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with invoicing.com.
 */
package com.ssy.invoicing.common.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.ssy.invoicing.order.bean.PurchaseProductBean;

/**
 * @author suoyao.shen
 *
 */
public class ExcelUtil {

    @SuppressWarnings("deprecation")
    public static void exportExcel(String title, String[] headers, List<PurchaseProductBean> dataset,
                                     String path) throws IOException {
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet(title);
        sheet.setDefaultColumnWidth((short) 18);
        // 设置表格默认列宽度为15个字节
        // 生成一个样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        // 设置这些样式
        headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 生成一个字体
        HSSFFont headerFont = workbook.createFont();
        headerFont.setColor(HSSFColor.BLACK.index);
        headerFont.setFontHeightInPoints((short) 12);
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 把字体应用到当前的样式
        headerStyle.setFont(headerFont);
        // 生成并设置另一个样式
        HSSFCellStyle rowStyle = workbook.createCellStyle();
        //rowStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        //rowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        rowStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        rowStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        rowStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        rowStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        rowStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        rowStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 生成另一个字体
        HSSFFont rowFont = workbook.createFont();
        rowFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        rowStyle.setFont(rowFont);
        /*
         * 生成excel数据
         */
        Row row;
        Cell cell;
        row = sheet.createRow((short)0);
        //生成头部
        for(int i = 0, len = headers.length; i < len; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(headerStyle);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //生成数据行
        int index = 1;
        for(PurchaseProductBean product : dataset) {
            HSSFRichTextString contractNumber = new HSSFRichTextString(product.getContractNumber());
            HSSFRichTextString ppattern = new HSSFRichTextString(product.getPattern());
            HSSFRichTextString saleCount = new HSSFRichTextString(String.valueOf(product.getSaleCount()));
            HSSFRichTextString salePrice = new HSSFRichTextString(product.getSalePrice());
            HSSFRichTextString deliverTime = new HSSFRichTextString(DateUtil.formateDate(product.getDeliverTime(), DateUtil.DATE_FORMAT));
            HSSFRichTextString remark = new HSSFRichTextString(product.getRemark());
            row = sheet.createRow(index++);
            cell = row.createCell(0);
            createRowCell(cell,rowStyle,contractNumber);
            cell = row.createCell(1);
            createRowCell(cell,rowStyle,ppattern);
            cell = row.createCell(2);
            createRowCell(cell,rowStyle,saleCount);
            cell = row.createCell(3);
            createRowCell(cell,rowStyle,salePrice);
            cell = row.createCell(4);
            createRowCell(cell,rowStyle,deliverTime);
            cell = row.createCell(5);
            createRowCell(cell,rowStyle,remark);
        }
        FileOutputStream fileOut = new FileOutputStream(path);
        workbook.write(fileOut);
        fileOut.close();
    }
    
    /**
     * 创建数据列样式
     * @param cell
     * @param style
     * @param value
     */
    public static void createRowCell(Cell cell,HSSFCellStyle style,HSSFRichTextString value) {
        cell.setCellStyle(style);
        cell.setCellValue(value);
    }
    
    public static void main(String[] args) {

    }
    
}
